package com.ruan.controller;

import com.ruan.common.Code;
import com.ruan.common.Result;
import com.ruan.pojo.ProjectToolAssignment;
import com.ruan.service.ProjectToolAssignmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/projectToolAssignment")
public class ProjectToolAssignmentController {
    @Autowired
    private ProjectToolAssignmentService projectToolAssignmentService;

    @PostMapping("/saveToolAssignmentInfo/{projectId}")
    public Result saveToolInfo(@RequestBody List<Map<String, String>> toolInfoArray,
                               @PathVariable String projectId) {
//        System.out.println("saveToolAssignmentInfo传来的参数是："+toolInfoArray);
        for (Map<String, String> toolInfo : toolInfoArray) {
            String id = toolInfo.get("id");
            String quantity = toolInfo.get("quantity");
            // 在这里处理存入数据库的逻辑
            ProjectToolAssignment projectToolAssignment = new ProjectToolAssignment();
            projectToolAssignment.setProjectId(Long.valueOf(projectId));
            projectToolAssignment.setToolId(Long.valueOf(id));
            projectToolAssignment.setToolAssignmentQuantity(Integer.valueOf(quantity));
            projectToolAssignmentService.save(projectToolAssignment);
        }

        return new Result(Code.SAVE_OK,"ToolAssignment数据保存成功");
    }
    //新增项目工具申请记录ResponseEntity<?>//HttpServletRequest request,
//    @PostMapping
//    public R<String> save(@RequestBody Map<String, Object> params) {
//        System.out.println("projectToolAssignment的参数有"+params);
//        Long projectId = Long.parseLong(params.get("projectId").toString());
//        List<String> toolIdStrings = (List<String>)params.get("toolId");//(List<Long>)
//        List<String> toolAssignmentStringQuantities = (List<String>) params.get("toolAssignmentQuantity");
//        System.out.println("参数2有"+projectId+toolIdStrings.toString()+toolAssignmentStringQuantities);
//        // 在这里处理前端发送的参数
//        List<Long> toolIds = toolIdStrings.stream()
//                .map(Long::parseLong)
//                .collect(Collectors.toList());
//
//        List<Integer> toolAssignmentQuantities = toolAssignmentStringQuantities.stream()
//                .map(Integer::parseInt)
//                .collect(Collectors.toList());
//
//        for (int i = 0; i < toolIds.size(); i++) {
//            Long toolId = toolIds.get(i);
//            System.out.println("看看遍历的参数"+toolId);
//            int toolAssignmentQuantity = toolAssignmentQuantities.get(i);
//
//            // 创建并保存ProjectToolAssignment对象
//            ProjectToolAssignment projectToolAssignment = new ProjectToolAssignment();
//            projectToolAssignment.setProjectId(projectId);
//            projectToolAssignment.setToolId(toolId);
//            projectToolAssignment.setToolAssignmentQuantity(toolAssignmentQuantity);
//            System.out.println("参数3是"+projectToolAssignment);
//            projectToolAssignmentService.save(projectToolAssignment);
//        }
//        return R.success("新增工具申请记录成功");
//    }
}
